<?php
require_once "../../php/config/config.php";
require_once "../model/adminDb.php";
$AdminDb = new AdminDb();
$name = $_SESSION['name'];
$dat = $AdminDb->query_userName($name);
$card = $dat[0]['card'];

$not_change = 0;
$revised = 1;
$data = $AdminDb->query_status_nr($not_change, $revised, $name);
$count = count($data);
$myData = $AdminDb->query_auther($name);
$myCount = count($myData);

?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/js/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/css/view.css"/>
    <title>管理后台</title>
    <style>
        #sumbitPerson {
            float: right;
        }

        #selectPerson {
            display: none;
        }

        #selectBox {
            margin-left: 0px;
        }

        .statusBox {
            width: 100%;
            color: white;
            background-color: #F0F2F5;
        }

    </style>

</head>
<body class="layui-view-body">
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md15">
                    <div class="layui-row layui-col-space15">
                        <div class="layui-col-md12">
                            <div class="layui-card">
                                <br>
                                <form class="layui-row layui-form " style="display: flex;flex-direction: row" action="">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">查询类型</label>
                                        <div class="layui-input-block " id="audio">
                                            <input name="vType" title="我的任务  <?php echo $count ?> 条" type="radio"
                                                   checked="" lay-filter="vType" value="0">
                                            <input name="vType" title="我创建的  <?php echo $myCount ?> 条" type="radio"
                                                   lay-filter="vType" value="1">
                                        </div>
                                    </div>

                                </form>
                                <div class="layui-card-body">
                                    <table id="test" lay-filter="test"></table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
<div class="layui-content" id="selectPerson">
    <div class="layui-row">
        <div class="layui-card">
            <div class="layui-card-header">选择负责人</div>
            <form class="layui-form layui-card-body" action="">

                <div class="layui-form-item">
                    <div class="layui-input-block" id="selectBox">
                        <select name="person1" id="person1" lay-filter="person1">
                            <option value="">请选择负责人</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <div class="layui-input-block" id="selectBox">
                        <textarea name="reason" id="reason" placeholder="请输入申请理由" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="float: right">
                        <a class="layui-btn layui-btn-blue" id="updatePerson" onclick="updatePerson()" lay-submit
                           lay-filter="updatePerson">立即提交</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<input type="text" name="userCard" id="userCard" disabled value="<?php echo $card ?>"
       style="display: none">

<input type="text" name="userName" id="userName" disabled value="<?php echo $name ?>"
       style="display: none">
<script id="toolbarDemo" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="refresh()"><i class="layui-icon">&#xe9aa;</i>
            刷新
        </button>
    </div>
</script>
<script src="../../static/js/jquery-3.3.1.min.js"></script>
<script src="../../static/js/layui/layui.all.js"></script>
<script src="../../static/js/config.js"></script>
<script>
    //定义初始值，查询变量，默认查询全部数据
    var sieve_auther = "0",
        sieve_person = "0",//未选择负责人
        my_create_count = "";//创建人
    //定义要修改负责人的id
    var personId = "";
    var personWin = "";//定义弹窗




    $(document).ready(function () {
        $userCard = $("#userCard").val();//获取身份类型
        $userName = $("#userName").val();//获取用户名
        sieve_auther = $userName;
        selectPerson();
        //未修改的
        $("#not_changed").click(function () {
            my_task();
        });
        //我创建的
        my_task();
        $("#new_bug").click(function () {
            myCreate();
        });

        layui.use(['form', 'table', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , laydate = layui.laydate;


            //监听单选框的值
            form.on("radio(vType)", function (data) {
                audio = data.value;
                $("#bugList").children().remove();
                switch (audio) {
                    case "0":
                        my_task();
                        break;
                    case "1":
                        myCreate();
                        break;
                }

            });

            form.render();
        });
    });


    //显示我创建的任务
    function myCreate() {
        auther = $userName;
        layui.use('table', function () {
            var table = layui.table;
            //第一个实例
            table.render({
                elem: '#test'
                , url: '../controller/my_create.php' //数据接口
                , page: true //开启分页
                , toolbar: "#toolbarDemo"
                , method: 'post'
                , limit: 100
                , limits: [20, 40, 60, 100]
                , where: {
                    auther: auther,
                }
                , cols: [[ //表头
                    {field: 'index', title: '序号', fixed: 'left', type: 'numbers'},
                    {field: 'id', title: 'id', width: 80, sort: true, fixed: 'left', toolbar: "#tableId"}
                    , {field: 'product', title: '产品', width: 100}
                    , {field: 'platfrom', title: '平台', width: 100}
                    , {field: 'person', title: '负责人', width: 100}
                    , {field: 'title', title: '标题', width: 250}
                    , {field: 'newName', title: '创建人', width: 100}
                    , {field: 'status', title: '状态', toolbar: '#status', width: 100}
                    , {field: 'addtime', title: '提交时间', toolbar: '#time', width: 180}
                    , {field: '', toolbar: '#barDemo',width:250 ,fixed:'right' ,title: '操作'}
                ]]
                , done: function (res, curr, count) {
                    $("#my_create").html(count);
                    if (res.count == 0) {
                        layer.msg("没有符合条件的数据哦", {time: 5000, icon: 0});
                    }
                }
            });


            //监听行工具事件
            table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'detail') {
                    $id = data.id;//获取当前一行的id
                    window.open('show_bug.php?id=' + $id, '_blank');
                } else if (layEvent === 'del') {
                    layer.confirm('真的删除行么', function (index) {

                        $id = data.id;//获取当前一行的id
                        delBug($id, index, obj);

                        //向服务端发送删除指令
                    });
                } else if (layEvent === 'edit') {
                    $id = data.id;//获取当前一行的id
                    window.open('edit_bug.php?id=' + $id, '_blank');
                }
                else if (layEvent === 'editPerson') {
                    $id = data.id;
                    personId = $id;
                    editPerson($id);

                }
            });

        });

    }

    //点击刷新
    function refresh() {

        window.location.reload();
    }

    //负责人选择框
    function selectPerson() {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/query_user.php',
            data: {},
            dataType: 'json',
            success: function (res) {
                $dat = res.data;
                $count = $dat.length;
                var select_person = document.getElementById("person1");
                for ($i = 0; $i < $count; $i++) {
                    select_person.innerHTML += "<option value='" + $dat[$i].name + "' >" + $dat[$i].name + "</option>";
                    var person1 = document.getElementById("person1").innerHTML += "<option value='" + $dat[$i].name + "' >" + $dat[$i].name + "</option>";

                }
                layui.use('form', function () {
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                    form.render('select', 'select_person'); //刷新select选择框渲染
                });


            },
            error: function (res) {
                console.log("加载失败");
            }


        });


    }

    //编辑新的负责人
    function editPerson(id) {
        personWin = layer.open({
            type: 1,
            shade: false,
            area: ['50%', '60%'], //宽高
            title: false, //不显示标题
            content: $('#selectPerson'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                $("#selectPerson").css({"display": "none"});
            }

        });
        $.ajax({
            type: "post",
            url: '../controller/query_bug.php',
            data: {
                "id": id,
            },
            dataType: "json",
            success: function (dat) {

                $val = $("#person1").val(dat.data[0].person);
                $reason = $("#reason").val(dat.data[0].reason);
                $("#selectPerson").css({"display": "none"});
            }, error: function (res) {
                layer.msg("查询负责人信息失败！");
            }


        });


    }

    //修改提交负责人
    function updatePerson() {

        $val = $("#person1").val();
        $reason = $("#reason").val();
        if ($val == "") {
            layer.msg("未选择负责人");
            return false;
        }
        else if ($reason < 2) {
            layer.msg("申请理由不得的少于三个字！");
            return false;

        }
        else {
            $.ajax({
                type: "post",
                url: '../controller/updatePerson.php',
                data: {
                    "id": personId,
                    "person": $val,
                    "reason": $reason,

                },
                dataType: "json",
                success: function (dat) {
                    layer.msg(dat.msg);
                    if (dat.code == 200) {
                        layer.close(personWin);
                        table(sieve_product, sieve_person, sieve_status, sieve_auther);
                        $("#selectPerson").css({"display": "none"});
                        var time = setInterval(showTime, 1000);
                        var i = 1;

                        function showTime() {

                            if (i == 0) {
                                window.location.reload();

                            }
                            i--;

                        }
                    }

                }, error: function (res) {
                    layer.msg("修改失败！");
                }


            });
        }


    }

    //删除
    function delBug(id, index, obj) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/del_bug.php',
            data: {
                "id": id
            },
            dataType: 'json',
            success: function (res) {
                layer.msg(res.msg);
                if (res.code == 200) {
                    layer.close(index);
                    obj.del(); //删除对应行（tr）的DOM结构
                }

            },
            error: function (res) {
                layer.msg("删除失败");
            }


        });

    }

    //显示我创建的任务
    function myCreate() {
        auther = $userName;
        layui.use('table', function () {
            var table = layui.table;


            //第一个实例
            table.render({
                elem: '#test'
                , url: '../controller/my_create.php' //数据接口
                , page: true //开启分页
                , toolbar: "#toolbarDemo"
                , method: 'post'
                , limit: 100
                , limits: [20, 40, 60, 100]
                , where: {
                    auther: auther,
                }
                , cols: [[ //表头
                    {field: 'id', title: 'id', width: 80, sort: true, fixed: 'left', toolbar: "#tableId"}
                    , {field: 'product', title: '产品', width: 100}
                    , {field: 'platfrom', title: '平台', width: 100}
                    , {field: 'person', title: '负责人', width: 100}
                    , {field: 'title', title: '标题', width: 250}
                    , {field: 'newName', title: '创建人', width: 100}
                    , {field: 'status', title: '状态', toolbar: '#status', width: 100}
                    , {field: 'addtime', title: '提交时间', toolbar: '#time', width: 180}
                    , {field: '', toolbar: '#barDemo',width:250 ,fixed:'right' ,title: '操作'}
                ]]
                , done: function (res, curr, count) {
                    $("#my_create").html(count);
                    if (res.count == 0) {
                        layer.msg("没有符合条件的数据哦", {time: 5000, icon: 0});
                    }
                }
            });


            //监听行工具事件
            table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'detail') {
                    $id = data.id;//获取当前一行的id
                    window.open('show_bug.php?id=' + $id, '_blank');
                } else if (layEvent === 'del') {
                    layer.confirm('真的删除行么', function (index) {

                        $id = data.id;//获取当前一行的id
                        delBug($id, index, obj);

                        //向服务端发送删除指令
                    });
                } else if (layEvent === 'edit') {
                    $id = data.id;//获取当前一行的id
                    window.open('edit_bug.php?id=' + $id, '_blank');
                }
                else if (layEvent === 'editPerson') {
                    $id = data.id;
                    personId = $id;
                    editPerson($id);

                }
            });

        });

    }

    //显示我的任务
    function my_task() {

        layui.use('table', function () {
            var table = layui.table;


            //第一个实例
            table.render({
                elem: '#test'
                , url: '../controller/my_task.php'//数据接口
                , page: true //开启分页
                , toolbar: "#toolbarDemo"
                , method: 'post'
                , limit: 100
                , limits: [20, 40, 60, 100]
                , where: {
                    auther: sieve_auther,
                }
                , cols: [[ //表头
                    {field: 'id', title: 'id', width: 80, sort: true, fixed: 'left', toolbar: "#tableId"}
                    , {field: 'product', title: '产品', width: 100}
                    , {field: 'platfrom', title: '平台', width: 100}
                    , {field: 'person', title: '负责人', width: 100}
                    , {field: 'title', title: '标题', max_width: 250}
                    , {field: 'newName', title: '创建人', width: 100}
                    , {field: 'status', title: '状态', toolbar: '#status', width: 100}
                    , {field: 'addtime', title: '提交时间', toolbar: '#time', width: 180}
                    , {field: '', toolbar: '#barDemo',width:250 ,fixed:'right' ,title: '操作'}
                ]]
                , done: function (res, curr, count) {
                    $("#my_rask").html(count);
                    if (res.count == 0) {
                        layer.msg("没有符合条件的数据哦", {time: 5000, icon: 0});
                    }
                }
            });


            //监听行工具事件
            table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'detail') {
                    $id = data.id;//获取当前一行的id
                    window.open('show_bug.php?id=' + $id, '_blank');
                } else if (layEvent === 'del') {
                    layer.confirm('真的删除行么', function (index) {

                        $id = data.id;//获取当前一行的id
                        delBug($id, index, obj);

                        //向服务端发送删除指令
                    });
                } else if (layEvent === 'edit') {
                    $id = data.id;//获取当前一行的id
                    window.open('edit_bug.php?id=' + $id, '_blank');
                }
                else if (layEvent === 'editPerson') {
                    $id = data.id;
                    personId = $id;
                    editPerson($id);

                }
            });

        });
    }

    //js将php时间戳转换成格式化日期
    function to_date(phpstr) {
        str = parseInt(phpstr) * 1000;//将php时间戳转化为整形并乘以1000
        var newDate = new Date(str);
        var year = newDate.getUTCFullYear();//取年份
        var month = newDate.getUTCMonth() + 1;//取月份
        var nowday = newDate.getUTCDate();//取天数
        var hours = newDate.getHours();//取小时
        var minutes = newDate.getMinutes();//取分钟
        var seconds = newDate.getSeconds();//取秒
        return year + "-" + month + "-" + nowday + " " + hours + ":" + minutes + ":" + seconds;//拼接 2017-2-21 12:23:43    }

    }
</script>

<script type="text/html" id="tableId">

    <a style="color: #787878" class="layui-table-link" lay-event="detail">{{d.id}}</a>
</script>
<script type="text/html" id="time">

    <a style="color: #787878" class="layui-table-link">{{to_date(d.addtime)}}</a>
</script>
<script type="text/html" id="status">


    {{#  if(d.status =="0"){ }}
    <div class="statusBox">
        <div style="width: 35%;background-color: #009688">
            <i class="layui-icon">&#xe642;</i>未修改
        </div>
    </div>
    {{#  } }}
    {{#  if(d.status ==1){ }}

    <div class="statusBox">
        <div style="width: 70%;background-color: #009688">
            <i class="layui-icon">&#xe642;</i>已修改
        </div>
    </div>
    {{#  } }}
    {{#  if(d.status ==2){ }}

    <div class="statusBox">
        <div style="width: 100%;background-color: #009688">
            <i class="layui-icon">&#xe605;</i>已完成
        </div>
    </div>
    {{#  } }}
    {{#  if(d.status ==3){ }}

    <div class="statusBox">
        <div style="width: 100%;background-color: #FF5722;text-align: center">待完成
        </div>
    </div>
    {{#  } }}
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    {{#  if(d.person ==$userName || $userCard==1){ }}

    <button class="layui-btn layui-btn-xs layui-btn-primary" lay-event="editPerson"><i class="layui-icon">&#xe642;</i>负责人
    </button>


    {{#  } }}

    {{#  if(d.status =="0" || d.status=="1" || d.status=="3"){ }}

    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>


    {{#  } }}
    {{#  if(d.status ==2){ }}
    <a class="layui-btn layui-btn-xs  layui-btn-disabled" lay-event="">编辑</a>
    {{#  } }}
    {{#  if($userCard ==1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{#  } }}

</script>

</body>


</html>